package svc

import (
	"go-weixin/app/preferential/api/internal/config"

	"go-weixin/app/preferential/rpc/preferentialclient"
	"go-weixin/app/usercenter/rpc/userrpcclient"
	"go-weixin/common/apiserver"
	"go-weixin/common/rpcserver"

	"github.com/zeromicro/go-zero/zrpc"
)

type ServiceContext struct {
	Config config.Config

	ApiMDLE         *apiserver.Middleware
	UserRpc         userrpcclient.UserCenter
	PreferentialRpc preferentialclient.Preferential
}

func NewServiceContext(c config.Config) *ServiceContext {
	rpcMDLE := rpcserver.NewMiddleware()
	preferentialRpc := preferentialclient.NewPreferential(zrpc.MustNewClient(zrpc.RpcClientConf{
		Endpoints: c.PreferentialRpc.Endpoints,
		NonBlock:  c.PreferentialRpc.NonBlock,
	}, zrpc.WithUnaryClientInterceptor(rpcMDLE.ClientRpcMiddleware)))
	userRpc := userrpcclient.NewUserRpcClient(zrpc.MustNewClient(zrpc.RpcClientConf{Endpoints: c.UserRpc.Endpoints, NonBlock: c.UserRpc.NonBlock}, zrpc.WithUnaryClientInterceptor(rpcMDLE.ClientRpcMiddleware)))
	return &ServiceContext{
		Config:          c,
		ApiMDLE:         apiserver.NewApiMiddleware(userRpc),
		UserRpc:         userRpc,
		PreferentialRpc: preferentialRpc,
	}
}
